package com.lx.controller;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;





import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lx.biz.IUser;
import com.lx.domain.AdminUser;
import com.lx.domain.DataGrid;
import com.lx.domain.Tree;



@Controller
public class UserController {

	@Resource 
	private IUser iUserimpl ;
	
	/*@RequestMapping("login")
	public String login(HttpServletRequest httpServletRequest){
		return "user/login";
	}*/
	
	/*@RequestMapping("main")
	public String mian(HttpServletRequest httpServletRequest){
		return "user/main";
	}
	
	@RequestMapping("user/check")
	public @ResponseBody String checkUser(String username ,HttpServletRequest httpServletRequest){
		 int result =iUserimpl.checkUser(username) ;
		if (result > 0) {
			return "success" ;
			
		}else {
			return "faile" ;
		}
		
		
	}
	
	
	@RequestMapping("user/logining")
	public String logining(String username ,String password ,HttpServletRequest httpServletRequest){
		AdminUser user = iUserimpl.findUser(username, password);
		if (user==null) {
			httpServletRequest.setAttribute("erro", "用户名或密码错误");
			return "user/login" ;
		}else {
			HttpSession session = httpServletRequest.getSession(true) ;
			session.setAttribute("user", user);
			if (user.getRole()> 0) {
				return "redirect:../emploee/emploeelist" ;
			}else {
				//跳入权限不足页面
				return "user/login" ; 
			}
			
		}
	}*/
	
	// 注销
	@RequestMapping("/loginout")
	public String loginOut(HttpServletRequest request) {
		HttpSession session = request.getSession(true);// 放置创建session
		if (session != null) {
			session.removeAttribute("user");
		}
		return "redirect:../login";
	}
	
	@RequestMapping("admin/manager")
	public String manager(HttpServletRequest httpServletRequest){
		return "admin/manager" ;
	}
	
	@RequestMapping("user/manager_2")
	public String manager_2(HttpServletRequest httpServletRequest){
		return "user/manager_2" ;
	}
	
	@RequestMapping("admin/user_datagrid")
	@ResponseBody
	public DataGrid datagrid(String rows ,String page , String sort ,String order ,String name ,String date_from ,String date_to ){
		List<AdminUser> list = iUserimpl.listUser(rows,page,sort,order,name,date_from,date_to);
		int count = iUserimpl.countUser(rows, page, sort, order, name, date_from, date_to);
		DataGrid  dg = new DataGrid() ;
        dg.setRows(list);
        dg.setTotal(count);
		return dg ;
	}
	
	
	//新增或者修改
	@RequestMapping("admin/user/user_update")
	@ResponseBody
	public String user_add(AdminUser user,String creatTime1 ,HttpServletRequest httpServletRequest){
		
		return iUserimpl.saveOrUpdateUser(user,creatTime1) ;
	}
	
	//管理页面tree返回
	@RequestMapping("admin/tree1")
	@ResponseBody
	public List<Tree> list_tree(String id){
		return iUserimpl.list_tree(id) ;
	}
	
	//删除用户
	@RequestMapping("admin/user/delete")
	@ResponseBody	
	public int user_delete(String ids){
		return iUserimpl.deleteUser(ids) ;
	}
}
